
var service = require('./service');

var express = require('express');

// 1. 创建一个路由容器
var router = express.Router();

router.get('/login', function (req, res) {
  res.render('login.html')
});

router.post('/login', function (req, res) {
  service.login(req.body, function (data) {
    if (data.code !=0) {
      return res.status(500).send('Server error.')
    }
    res.redirect('/list')
  })
});

router.get('/new', function (req, res) {
  res.render('new.html')
});
router.post('/new', function (req, res) {
  service.saveNews(req.body, function (err) {
    res.redirect('/list')
  })
});

router.get('/list', function (req, res) {
  service.find(function (list) {
    res.render('list.html', {
      list: list
    })
  })
});

router.get('/add', function (req, res) {
  res.render('add.html')
})

router.post('/add', function (req, res) {
  service.save(req.body, function (err) {
    res.redirect('/login')
  })
});



router.get('/delete', function (req, res) {
  service.deleteById(req.query.id, function (err) {
    res.redirect('/list')
  })
})





// 3. 把 router 导出
module.exports = router;

